// ****************************************************************************
// *     Filename:  DetectorConstruction.cc
// *      Created:  2012-09-14 11:34:20
// *      Version:  1.0
// *     Revision:  0
// *       Author:  Tran Hoai Nam (nam), nam@kuno-g.phys.sci.osaka-u.ac.jp
// * Organization:  Osaka University
// *      License:  Geant4 Software License 
// *                http://geant4.cern.ch/license/LICENSE.html
// *  Description:  
// ****************************************************************************
#include "DetectorConstruction.hh"
#include "MySD.hh"
//Material and geometry
#include "G4Material.hh"
#include "G4NistManager.hh"     
#include "G4Box.hh"
#include "G4Tubs.hh"
#include "G4LogicalVolume.hh"
#include "G4ThreeVector.hh"
#include "G4PVPlacement.hh"
#include "G4Sphere.hh"
#include "G4SDManager.hh"
#include "G4Colour.hh"
#include "G4VisAttributes.hh"
#include "G4RotationMatrix.hh"
#include "G4SubtractionSolid.hh"

#include "G4RunManager.hh"
//Field 
#include "G4FieldManager.hh"
#include "G4TransportationManager.hh"
#include "G4UniformMagField.hh"
#include "G4ChordFinder.hh"
#include "G4Mag_UsualEqRhs.hh"
#include "G4ClassicalRK4.hh"

#include "globals.hh"

DetectorConstruction::DetectorConstruction() : fpPhysWorld(0)
{
	DefineMaterials();
	SetDefaultParameters();
	CalculateSize();
}

DetectorConstruction::~DetectorConstruction()
{; }

void DetectorConstruction::DefineMaterials()
{
	G4NistManager* man = G4NistManager::Instance();
	fVacuum = man->FindOrBuildMaterial("G4_Galactic");
	fAir = man->FindOrBuildMaterial("G4_AIR");
	fCopper = man->FindOrBuildMaterial("G4_Cu");
	fAluminum = man->FindOrBuildMaterial("G4_Al");
	fNaI = man->FindOrBuildMaterial("G4_SODIUM_IODIDE");
	fSilicon = man->FindOrBuildMaterial("G4_Si");
	fLead = man->FindOrBuildMaterial("G4_Pb");
	fPScinti = man->FindOrBuildMaterial("G4_PLASTIC_SC_VINYLTOLUENE");

	G4double density;
	G4int ncomponents;
	G4double fractionmass;
	fSSteel = new G4Material("StainlessSteel", density= 8.06*g/cm3, ncomponents=6);
	G4Element *C = man->FindOrBuildElement("C");
	G4Element *Si = man->FindOrBuildElement("Si");
	G4Element *Cr = man->FindOrBuildElement("Cr");
	G4Element *Mn = man->FindOrBuildElement("Mn");
	G4Element *Fe = man->FindOrBuildElement("Fe");
	G4Element *Ni = man->FindOrBuildElement("Ni");
	fSSteel->AddElement(C, fractionmass=0.001);
	fSSteel->AddElement(Si, fractionmass=0.007);
	fSSteel->AddElement(Cr, fractionmass=0.18);
	fSSteel->AddElement(Mn, fractionmass=0.01);
	fSSteel->AddElement(Fe, fractionmass=0.712);
	fSSteel->AddElement(Ni, fractionmass=0.09);
}

void DetectorConstruction::CalculateSize()
{
}

void DetectorConstruction::SetDefaultParameters()
{
	fWorldX = 10*cm;
	fWorldY = 10*cm;
	fWorldZ = 10*cm;
}

G4VPhysicalVolume* DetectorConstruction::Construct()
{
	
	G4Box* BoxWorld
		= new G4Box("Worldbox",fWorldX/2,fWorldY/2,fWorldZ/2);
	G4LogicalVolume *logWorld;
	logWorld = new G4LogicalVolume(BoxWorld, fVacuum,"Worldlog",0,0,0); 
	fpPhysWorld = new G4PVPlacement(0,G4ThreeVector(),
			logWorld,"expHall",0,false,0);
  //logWorld->SetVisAttributes(G4VisAttributes::Invisible); //Set invisible
	
	G4double fDet1X = 5*cm;
	G4double fDet1Y = 5*cm;
	G4double fDet1Z = 2*mm;

	G4Box* BoxDet1 = new G4Box("Det1Box", fDet1X/2, fDet1Y/2, fDet1Z/2);
	G4LogicalVolume* logDet1 = new G4LogicalVolume(BoxDet1, fSilicon, "logDet1");

	new G4PVPlacement(0, G4ThreeVector(0.,0.,- 1*cm), 
			logDet1,"physDet1",logWorld,false,0);

	//new G4PVPlacement(0, G4ThreeVector(0.,0.,0. + 2*cm), 
			//logDet1,"physDet1",logWorld,false,1);

  G4VisAttributes* green = new G4VisAttributes(G4Colour::Green());
  logDet1->SetVisAttributes(green);

	//Sensitive detector definition
	G4SDManager* SDman = G4SDManager::GetSDMpointer();
	G4VSensitiveDetector* aSD = new MySD("aSD"); 
	SDman->AddNewDetector(aSD);
	logDet1->SetSensitiveDetector(aSD);

	return fpPhysWorld;
}
